require 'View/UIBase/UIBase'

UICommonSource = UIBase.New(PanelResNames.UICommonSource)

function UICommonSource.Init(func, showSourceList, obj)
    UICommonSource.showSourceList = showSourceList;
    UICommonSource.goFunc = func;
    UICommonSource.clickObj = obj
    UIBase.Init(UICommonSource);
end

function UICommonSource:OnCreate()
    self.sourceUIobj = self:GetChild("Bg");
    self.text1 = self:GetChild("text1");
    self.list = self:GetChild("list");
    self.title = self:GetChild("title");
    self.btnClose = self:GetChild("btnClose");
    self.nothingController = self:GetController("nothing");

    self:addEvent();

    self:initView();
end

function UICommonSource:addEvent()
    self.btnClose.onClick:Add(self.onClose, self);
end

function UICommonSource:onClose()
    self:OnBack();
end

function UICommonSource:removeEvent()
    self.btnClose.onClick:Clear();
end

function UICommonSource:initView()
    local sourceUI = {};
    if self.showSourceList ~= nil and #self.showSourceList > 0 then
        local itemTableData;
        local goTableList;
        local sourceData;
        for k, v in pairs(self.showSourceList) do
            itemTableData = G.dataTable["t_item"][tostring(v)];
            if itemTableData ~= nil then
                goTableList = {};
                if itemTableData.f_source ~= "" then
                    goTableList = decodeJsonStrToTabel(itemTableData.f_source);
                    if logicMgr.MapManager.GetCurrentStage() < 532 then
                        table.sort(goTableList, function (a, b)
                            if a[2] == 0 or b[2] == 0 then return a[2] == 0; end
                            return a[2] > b[2];
                        end);
                    else
                        table.sort(goTableList, function (a, b) return a[2] > b[2] end);
                    end
                    for key, value in pairs(goTableList) do
                        local data = {};
                        data.itemId = v;
                        sourceData = G.dataTable["t_source"][tostring(value[1])];
                        data.recommend = value[2];
                        data.state = 0;
                        if sourceData ~= nil then
                            if sourceData.f_type == 1 and logicMgr.ItemManager.getBagNumByTableID(sourceData.f_key) > 0 and not isOpenUI(PanelNames.UIBag) then
                                data.state = 1
                            elseif sourceData.f_type == 2 then
                                data.state = 1
                            end
                            if data.state >= 1 then
                                data.sourceId = value[1];
                                table.insert(sourceUI, data);
                            end
                        end
                    end
                end
            end
        end
    end

    if #sourceUI > 0 then
        self.nothingController.selectedIndex = 0;
        self.list:RemoveChildrenToPool();
        for i = 1, #sourceUI do
            local item = self.list:AddItemFromPool();
            self:updateSourceItem(item, sourceUI[i]);
        end
        self.list:ScrollToView(0);
    else
        self.nothingController.selectedIndex = 1;
        self.title.text = getLanguage("SourceTips4")
    end

    if self.clickObj then
        local screenPos = self.clickObj.parent:TransformPoint(self.clickObj.xy, self.sourceUIobj.parent);
        local y = math.max(screenPos.y + self.clickObj.height + 10, 0);
        if self.sourceUIobj.height + y > GRoot.inst.height then
            y = math.max(screenPos.y - self.sourceUIobj.height - 10, 0);
        end
        self.sourceUIobj.y = y
    else
        self.sourceUIobj.y = GRoot.inst.height / 2 - self.sourceUIobj.height / 2
    end
end

function UICommonSource:updateSourceItem(obj, data)
     local title = obj:GetChild("title");
    local item = obj:GetChild("item");
    local tips = obj:GetChild("tips");
    local btn_Go = obj:GetChild("btn_Go");
    local isRecommendController = obj:GetController("ifRecommend");
    local isFunc = obj:GetController("ifFunc");
    local typeController = obj:GetController("type");
    title.text = logicMgr.ItemManager.GetItemName(data.itemId);
    logicMgr.CostManager.RefreshItem(item, {data.itemId});
    btn_Go.onClick:Clear();
    if data.recommend == 0 then
        isRecommendController.selectedIndex = 1;
    else
        isRecommendController.selectedIndex = 0;
    end
    if data.sourceId == nil then
        tips.text = getLanguage("SourceTips3");
    else
        local sourceData = G.dataTable["t_source"][tostring(data.sourceId)];
        if sourceData == nil then
            tips.text = getLanguage("SourceTips3");
            btn_Go.onClick:Add(function()
                UISysTips.AddMsg(getLanguage("SourceTips3"));
            end);
        else
            local key = sourceData.f_key;
            local isClose = sourceData.f_close;
            if sourceData.f_type == 1 then
                typeController.selectedIndex = 0
                btn_Go.onClick:Add(function()
                    if data.state >= 1 then
                        local _data = logicMgr.ItemManager.getItemConstDatabyModelId(key, true);
                        _data.itemId = logicMgr.ItemManager.getItemIdByModelId(key);
                        showUI(PanelNames.UIBagItemInfo, _data, 1);
                    else
                        UISysTips.AddMsg(getLanguage("SourceTips2"));
                    end
                end);
                tips.text = string.format(getLanguage("SourceTips1", nil, "%s"),logicMgr.ItemManager.GetItemName(key));
            else
                typeController.selectedIndex = 1
                if logicMgr.FuncOpenManager.isOpen(key) then
                    isFunc.selectedIndex = 0;
                else
                    isFunc.selectedIndex = 1;
                end
                btn_Go.onClick:Add(function()
                    if logicMgr.FuncOpenManager.isOpen(key, true, obj) then
                        if UICommonSource.goFunc and isClose ~= 1 then
                            UICommonSource.goFunc();
                        end
                        logicMgr.FuncOpenManager.jump(key, true);
                        UICommonSource:onClose();
                    end
                end);
                tips.text = string.format(getLanguage("SourceTips1", nil, "%s"),getLanguage(sourceData.f_FunctionName));
            end
        end
    end
end

-- 隐藏
function UICommonSource:OnDisable()
end

-- 卸载
function UICommonSource:OnDestroy()
    self:removeEvent();
end

return UICommonSource